package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.mapper.CheckItemMapper;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.service.CheckItemService;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

// 2.7.8
@Service
@Transactional   //  mybatis plus   +  spring data jpa
public class CheckItemServiceImpl extends ServiceImpl<CheckItemMapper, CheckItem> implements CheckItemService {
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Page pageResult = null;
        //  将分页查询结果集  封装到 自定义的分页结果集
        QueryWrapper<CheckItem> queryWrapper = new QueryWrapper<CheckItem>();
        queryWrapper.eq("is_delete",0);  //  查询有效的数据
        if(StringUtils.isNotBlank(queryPageBean.getQueryString())){
            //  项目名称 模糊查询   有条件分页查询    条件 封装  添加构造器对象中
             queryWrapper.like("name",queryPageBean.getQueryString());
            pageResult = page(new Page<CheckItem>(queryPageBean.getCurrentPage(),queryPageBean.getPageSize()),queryWrapper);
        }else{
            //  无条件的分页查询 业务层实现
            pageResult = page(new Page<CheckItem>(queryPageBean.getCurrentPage(),queryPageBean.getPageSize()),queryWrapper);
        }
        return new PageResult(pageResult.getTotal(),pageResult.getRecords());
    }

    @Override
    public boolean updateCheckItemById(int id) {
        // 逻辑修改
        CheckItem checkItem = new CheckItem();
        checkItem.setIs_delete(1);
        checkItem.setId(id);
        boolean b = updateById(checkItem); //  mp  更新操作  实现 逻辑删除
        return b;
    }

    @Override
    public List<CheckItem> findCheckItemByGroupId(Integer gid) {
        return baseMapper.findCheckItemByGroupId(gid);
    }
}
